### Load data & packages --------
rm(list = ls(all.names = TRUE)) 
gc()

# Load functions
source("04a_supporting_functions_en.R", encoding = "UTF-8")

networks_strong <- 
  readRDS('intermediary_outputs/list_networks_strong_out_4.Rds')

# Generate graph examples of social status measures
par(mfrow = c(1,1))

# Set network example:
i = 117

gg <- networks_strong[[i]]
gg <- gg %s% which(is.na(gg %v% "negro_na") == F)
gg <- gg %s% which(gg %v% "ssi_all_race" > 0)

gg %v% "color" <- c("gold", "tomato")[gg %v% "negro_na" + 1]
gg %v% "shape" <- c(50, 50)[gg %v% "negro_na"]
gg %v% "id" <-seq(1, network.size(gg))

## 9 and 13 are the ones to edit.  

edit <- gg %v% "shape"
edit[9] <- 4
edit[10] <- 4
set.seed(1234)

# Fixing coordenates:
trial <- gplot(gg, gmode = "graph", 
      label =  round(gg %v% "id",2),
      vertex.col = gg %v% "color",
      vertex.sides = edit,
      vertex.cex = 2.2,  
      displayisolates = T,
      label.pos = 5,
      vertex.enclose	= F,
      main = "All races social status")

  ### Panel 2a - Same race social status
to_remove<- get.edgeIDs(gg, 4)[which(get.edgeIDs(gg, 4) != get.edgeIDs(gg, 6))] 

remove_edges <- c(to_remove, get.edgeIDs(gg, 7), get.edgeIDs(gg, 5))
gg_same <- gg
gg_same <- delete.edges(gg_same, remove_edges)


## Recompute SSI:
negro_new <- gg %v% "negro_na"


ssi_same_new <- ssi_net(gg_same, "negro_na")
gg_same %v% "ssi_same_new" <- ssi_same_new

### Panel 3a - Other race social status
gg_other <- gg
negro_new <- gg %v% "negro_na" + 1
negro_new[9] <- 1
negro_new[10] <- 1
gg_other %v% "negro_new" <- negro_new
gg_other %v% "color_new" <- c("gold", "tomato")[gg_other %v% "negro_new"]
gg_other %v% "negro_na" <- gg_other %v% "negro_na" - 1

  ## Removing edges:
to_remove_1 <- get.edgeIDs(gg_other, 4)[
  which(!(get.edgeIDs(gg_other, 4) %in% get.edgeIDs(gg_other, 9)) &
          !(get.edgeIDs(gg_other, 4) %in% get.edgeIDs(gg_other, 6)))]

to_remove_2 <- get.edgeIDs(gg_other, 9)[which(!get.edgeIDs(gg_other, 9) %in% 
                                                get.edgeIDs(gg_other, 4))]

to_remove_3 <- get.edgeIDs(gg_other, 7)[which(get.edgeIDs(gg_other, 7) != 
                                                get.edgeIDs(gg_other, 10))]

to_remove <- unique(c(to_remove_1, to_remove_2, to_remove_3))
gg_other <- delete.edges(gg_other, to_remove)

ssi_other_new <- ssi_net(gg_other, "negro_new")

gg_other %v% "vertex.names"
gg_other %v% "id"

names <- as.numeric(gg_other %v% "vertex.names")
ssi_other_new_order <- rep(NA, network.size(gg_other))
names_correct_order <- rep(NA, network.size(gg_other))

for (i in 1:length(names)) {
  # i = 3
  ssi_other_new_order[i] <- ssi_other_new[which(as.numeric(names(ssi_other_new)) == names[i])]
}

set.seed(1234)

# All race
pdf("figures/ex_ssi_all_race.pdf")

gplot(gg, gmode = "graph", 
      label =  round(gg %v% "ssi_all_race",2),
      vertex.col = gg %v% "color",
      vertex.sides = gg %v% "shape",
      vertex.cex = 3.5,  
      displayisolates = T,
      label.pos = 5,
      label.cex = 2.0,
      vertex.enclose	= F,
      coord = trial)

dev.off()

# Same race
pdf("figures/ex_ssi_same_race.pdf")

gplot(gg_same, gmode = "graph", 
      label =  round(gg_same %v% "ssi_same_race_negro_na",2),
      vertex.col = gg_same %v% "color",
      vertex.sides = gg %v% "shape",
      vertex.cex = 3.5,  
      displayisolates = T,
      label.pos = 5,
      label.cex = 2.0,
      vertex.enclose	= F,
      coord = trial)

dev.off()

# Other race - Change
pdf("figures/ex_ssi_other_race_change.pdf")

gplot(gg_other, gmode = "graph", 
      label =  round(ssi_other_new_order,2),
      vertex.col = gg_other %v% "color_new",
      vertex.sides = edit,
      vertex.cex = 3.5,  
      displayisolates = T,
      label.pos = 5,
      label.cex = 2.0,
      vertex.enclose	= F,
      coord = trial)

dev.off()

# Other race - Final
pdf("figures/ex_ssi_other_race_final.pdf")

gplot(gg, gmode = "graph", 
      label =  round(gg %v% "ssi_other_race_negro_na_2",2),
      vertex.col = gg %v% "color",
      vertex.cex = 3.5,  
      displayisolates = T,
      label.pos = 5,
      label.cex = 2.0,
      vertex.enclose	= F,
      coord = trial)

dev.off()

